package com.medical.wechat.web.controller;

import com.medical.common.Constant;
import com.medical.common.JsonResult;
import com.medical.wechat.model.WechatAccount;
import com.medical.wechat.service.WechatAccountService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2016/10/21.
 */
@Controller
@RequestMapping("/wechatAccount")
public class WechatAccountController {
    @Autowired
    private WechatAccountService wechatAccountService;

    //查询wechataccount全部信息
    @ApiOperation(value="查询wechataccount全部信息", notes="查询wechataccount全部信息")
    @RequestMapping(value = "",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult selectWechatAccount() {
        try {
            return new JsonResult("200", "", wechatAccountService.findAll());
        } catch (Exception e) {
            return new JsonResult("500", e.getMessage());
        }
    }
    //根据id查询wechataccount对象
    @ApiOperation(value="根据id查询wechataccount对象", notes="根据id查询wechataccount对象")
    @RequestMapping(value = "/get/{id}",method = RequestMethod.POST)
    public JsonResult selectById(@PathVariable String id){
        try {
        WechatAccount wechatAccount=wechatAccountService.findById(id);
        return new JsonResult("200","",wechatAccount);
    } catch (Exception e) {
        return new JsonResult("500", e.getMessage());
    }
    }
    //新增(先不要管12-05)
//    @ApiOperation(value="新增", notes="新增")
    @RequestMapping(value = "/post",method = RequestMethod.POST)
    @ResponseBody
    //name; originalId; appId; appSecrect; accessToken; encodingAesKey organizationId;
    // qrcode; Date tokenExpired; ticket; createdBy;

    public JsonResult postWechatAccount(@RequestParam(value = "name",required = false) String name ,
                                        @RequestParam(value = "originalId",required = false) String originalId ,
                                        @RequestParam(value = "appId",required = false) String appId ,
                                        @RequestParam(value = "appSecrect",required = false) String appSecrect ,
                                        @RequestParam(value = "accessToken",required = false) String accessToken ,
                                        @RequestParam(value = "encodingAesKey",required = false) String encodingAesKey ,
                                        @RequestParam(value = "organizationId",required = false) String organizationId ,
                                        @RequestParam(value = "qrcode",required = false) String qrcode ,
                                        @RequestParam(value = "tokenExpired",required = false) String tokenExpired ,
                                        @RequestParam(value = "ticket",required = false) String ticket,
                                        @RequestParam(value = "createdBy",required = false) String createdBy) {
        try {
            WechatAccount wechatAccount=new WechatAccount();
            wechatAccount.setName(name);
            wechatAccount.setOriginalId(originalId);
            wechatAccount.setAppId(appId);
            wechatAccount.setAppSecrect(appSecrect);
            wechatAccount.setAccessToken(accessToken);
            wechatAccount.setEncodingAesKey(encodingAesKey);
            wechatAccount.setOrganizationId(organizationId);
            wechatAccount.setQrcode(qrcode);
            Date tokenExpired1 = new Date();
            try {
                tokenExpired1 = DateUtils.parseDate(tokenExpired, Constant.DATE_PATTERN);
            }catch (ParseException ex){
                ex.printStackTrace();
            }
            wechatAccount.setTokenExpired(tokenExpired1);
            wechatAccount.setTicket(ticket);
            wechatAccount.setCreatedBy(createdBy);
            wechatAccount.setCreatedTime(new Timestamp(System.currentTimeMillis()));
            wechatAccountService.createWechatAccount(wechatAccount);
            return new JsonResult("200", "", wechatAccount);
        } catch (Exception e) {
            return new JsonResult("500", e.getMessage());
        }
    }

    //编辑
    @ApiOperation(value="编辑", notes="编辑")
    @RequestMapping(value = "/patch/{id}",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult patchWechatAccout(@PathVariable String id,
                                        @RequestParam(value = "name",required = false) String name ,
                                        @RequestParam(value = "appId",required = false) String appId ,
                                        @RequestParam(value = "appSecrect",required = false) String appSecrect ,
                                        @RequestParam(value = "accessToken",required = false) String accessToken ,
                                        @RequestParam(value = "encodingAesKey",required = false) String encodingAesKey ,
                                        @RequestParam(value = "organizationId",required = false) String organizationId ,
                                        @RequestParam(value = "qrcode",required = false) String qrcode) {
        try {
            WechatAccount wechatAccount=wechatAccountService.findById(id);
            wechatAccount.setName(name);
            wechatAccount.setAppId(appId);
            wechatAccount.setAppSecrect(appSecrect);
            wechatAccount.setAccessToken(accessToken);
            wechatAccount.setEncodingAesKey(encodingAesKey);
            wechatAccount.setOrganizationId(organizationId);
            wechatAccount.setQrcode(qrcode);
            Date tokenExpired1 = new Date();
//            try {
//                tokenExpired1 = DateUtils.parseDate(tokenExpired, Constant.DATE_PATTERN);
//            }catch (ParseException ex){
//                ex.printStackTrace();
//            }
            wechatAccount.setTokenExpired(tokenExpired1);
            wechatAccountService.updateWechatAccountById(wechatAccount);
            return new JsonResult("200", "", wechatAccount);
        } catch (Exception e) {
            return new JsonResult("500", e.getMessage());
        }
    }


    //删除
    @ApiOperation(value="删除", notes="删除")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult deleteWechatAccount(@PathVariable String id){
        try {
            wechatAccountService.deleteById(id);
            return new JsonResult("200","删除成功");
        }catch (Exception e){
            return new JsonResult("500",e.getMessage());
        }

    }

}
